<?php

/**
 * 七天后自动收货
 */
namespace App\Console\Commands;

use App\Models\Orders;
use App\Models\ProfitLogs;
use App\Models\Stores;
use App\Models\Users;
use Illuminate\Console\Command;

class OrderTimingTask extends Command
{
    protected $name = 'order_timing_task';//命令名称

    public function handle()
    {
        $orders = Orders::where("shipping_status", 2) //已发货
        ->where("order_status", 1) //未收货
        ->where("order_state", 4) //待收货
        ->whereDate("shipping_time", "<", date("Y-m-d H:i:s", strtotime("-7 days")))//发货时间已过七天
        ->get(['id','store_id','pay_price']);
        foreach ($orders as $key => $val){
            Orders::where('id',$val['id'])->update(["order_status"=>2,"order_state"=>5,"order_time"=>date("Y-m-d H:i:s"),"finnshed_time"=>date("Y-m-d H:i:s")]);
            //HR确认订单，供应商获得收益
            $member_info = Stores::where('id',$val['store_id'])->first(['user_id','seller_name']);
            Users::where('id',$member_info['user_id'])->increment('profit',$val['pay_price']);
            //收益日志记录
            $users = Users::where('id',$member_info['user_id'])->first(['id','username']);
            $order_goods = Orders::where('order_id',$val['id'])->pluck('goods_name')->toArray();
            if (!empty($order_goods)){
                $tit = '商品名称:'.implode('、',$order_goods);
            }else{
                $tit = '购买商品';
            }
            $profit = [
                'user_id' => $users['id'],
                'user_name' => $users['username'],
                'profit' => '+'.$val['pay_price'],
                'desc' => $tit,
                'type' => 1,
            ];
            ProfitLogs::create($profit);
        }
    }
}